package com.kaifamiao.dispatch;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

@WebServlet( "/liu/yi/fei" )
public class LiuYiFeiServlet extends HttpServlet {

    private Logger logger = Logger.getLogger( this.getClass().getName() );

    @Override
    protected void service(HttpServletRequest request , HttpServletResponse response )
            throws ServletException, IOException {
        logger.info( "我是刘亦菲" );

        // 在 请求对象(request) 中设置 属性(attribute)
        request.setAttribute( "id" , 12345 );
        request.setAttribute( "name" , "张三丰" );

        logger.info( "id: " + request.getAttribute( "id" ) );
        logger.info( "name: " + request.getAttribute( "name" ) );

        // 指定目标资源路径
        String path = "/wang/yu/yan" ;
        // 获得可以去往指定路径的 请求指派器
        RequestDispatcher dispatcher = request.getRequestDispatcher( path );
        // 通过 请求指派器 将 请求 和 响应 派遣给 指定的资源
        dispatcher.forward( request , response );

    }
}
